package com.heima.kafka.sample;

import org.apache.kafka.clients.producer.*;

import java.util.Properties;
import java.util.concurrent.ExecutionException;

/**
 * create 2023/6/23 10:04
 */


public class ProducerQuickStart {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Properties prop = new Properties();
        prop.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.200.130:9092");
        prop.put(ProducerConfig.RETRIES_CONFIG,5);
        //ack配置  消息确认机制
        prop.put(ProducerConfig.ACKS_CONFIG,"all");
        //数据压缩
        prop.put(ProducerConfig.COMPRESSION_TYPE_CONFIG,"lz4");
        prop.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
        //消息value的序列化器
        prop.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");


        KafkaProducer<String,String> kafkaProducer = new KafkaProducer<String,String>(prop);

        //3.发送消息
        /**
         * 第一个参数 ：topic
         * 第二个参数：消息的key
         * 第三个参数：消息的value
         */
        for (int i = 0; i < 5; i++) {
            ProducerRecord<String,String> kvProducerRecord = new ProducerRecord<String,String>("itcast-topic-input","hello kafka");
            kafkaProducer.send(kvProducerRecord);
        }


//        ProducerRecord<String, String> producerRecord = new ProducerRecord<String, String>("itheima-topic", "1001", "hello kafka");
  /*      RecordMetadata recordMetadata = kafkaProducer.send(producerRecord).get();
        System.out.println(recordMetadata.offset());*/

        /*for (int i=0;i<5;i++) {
            kafkaProducer.send(producerRecord, new Callback() {
                @Override
                public void onCompletion(RecordMetadata recordMetadata, Exception e) {
                    if (e != null) {
                        System.out.println("记录异常信息到日志表中");
                    }
                    System.out.println(recordMetadata.offset());
                }
            });
        }*/
        kafkaProducer.close();
    }
}
